package com.igoal.campaign.xmas2010.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.campaign.xmas2010.dto.XmasWishDTO;
import com.igoal.dto.UserDTO;
import com.igoal.utils.ConvertUtils;
import com.igoal.utils.RandomString;
import com.igoal.utils.StringUtils;

@Controller
public class CardController extends BaseController {

	@RequestMapping(method = RequestMethod.GET, value = "/xmas.html")
	public String showCard(ModelMap modelMap,
			@RequestParam(required = false, value = "id") String code) {

		XMASModel model = new XMASModel();
		String view;

		if (code != null && code.length() > 10) {
			// view card
			view = DEFAULT_CARD_PUBLIC_VIEW;
			code = code.substring(5);
			code = code.substring(0, code.length() - 5);
			Long id = ConvertUtils.toLong(code, 0, 16);
			showCardPublic(modelMap, model, id);
		} else {
			// create card
			view = DEFAULT_CREATE_CARD_VIEW;
			showCardForm(modelMap, model);
		}

		if (isLoginUser()) {
			model.setUserInSession(getUserInSession());
		}

		modelMap.addAttribute("xmas", model);

		return getViewPrefix() + view;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/xmas.html")
	public String createCard(ModelMap modelMap, @ModelAttribute("xmas") XMASModel model,
			HttpServletRequest request) {
		String username = getUserInSession();

		XmasWishDTO wishCard = model.getCard();

		wishCard = cardService.saveXmasWish(wishCard, username);

		String port = request.getServerPort() == 80 ? "" : (":" + request.getServerPort());
		String url = "http://" + request.getServerName() + port + request.getContextPath()
				+ "/xmas/" + RandomString.generate(5) + Long.toString(wishCard.getId(), 16)
				+ RandomString.generate(5);

		Reception r1 = model.getReception1();
		if (StringUtils.isEmailValid(r1.getEmail())) {
			mailService.sendXmasCard(username, r1.getName(), r1.getEmail(), url);
		}

		Reception r2 = model.getReception2();
		if (StringUtils.isEmailValid(r2.getEmail())) {
			mailService.sendXmasCard(username, r2.getName(), r2.getEmail(), url);
		}

		Reception r3 = model.getReception3();
		if (StringUtils.isEmailValid(r3.getEmail())) {
			mailService.sendXmasCard(username, r3.getName(), r3.getEmail(), url);
		}

		Boolean sendToMe = model.getSendToMe();
		if (sendToMe) {
			if (isLoginUser()) {
				String userList = "";
				if (r1 != null && StringUtils.isNotBlank(r1.getName())) {
					userList += r1.getName().trim();
				}

				if (r2 != null && StringUtils.isNotBlank(r2.getName())) {
					if (userList.length() > 0) {
						userList += ", " + r2.getName().trim();
					} else {
						userList += r2.getName().trim();
					}
				}

				if (r3 != null && StringUtils.isNotBlank(r3.getName())) {
					if (userList.length() > 0) {
						userList += ", " + r3.getName().trim();
					} else {
						userList += r3.getName().trim();
					}
				}

				if (StringUtils.isBlank(userList)) {
					userList = "iGoal.vn";
				}

				UserDTO user = getUserInSessionInfo();
				mailService.sendXmasCardReport(user.getUsername(), userList, user.getEmail(), url);
			}
		}

		if (isLoginUser()) {
			model.setUserInSession(getUserInSession());
		}

		model.setCard(wishCard);
		modelMap.addAttribute("xmas", model);
		modelMap.addAttribute("url", url);

		return getViewPrefix() + DEFAULT_CREATE_CARD_SUCCESSFUL_VIEW;
	}

	public void showCardForm(ModelMap modelMap, XMASModel model) {
	}

	public void showCardPublic(ModelMap modelMap, XMASModel model, Long id) {
		XmasWishDTO wish = cardService.getXmasWish(id);
		model.setCard(wish);
	}
}
